﻿@page "/fetchdata"

<PageTitle>Weather forecast</PageTitle>

@inject WeatherForecastService ForecastService

<h1>Weather forecast</h1>

<p>This component demonstrates fetching data from a service.</p>

<MDataTable Headers="_headers" Loading="_loading" Items="forecasts" ItemsPerPage="5" Class="elevation-1">
    <ItemColContent>
        @if (context.Header.Value == nameof(WeatherForecast.Date))
        {
            @context.Item.Date.ToShortDateString()
        }
        else
        {
            @context.Value
        }
    </ItemColContent>
</MDataTable>

@code {
    private bool _loading;
    private List<DataTableHeader<WeatherForecast>> _headers = new List<DataTableHeader<WeatherForecast>> {
        new (){ Text = "Date",Value = nameof(WeatherForecast.Date) },
        new (){ Text = "Temp. (C)",Value = nameof(WeatherForecast.TemperatureC) },
        new (){ Text = "Temp. (F)",Value = nameof(WeatherForecast.TemperatureF) },
        new (){ Text = "Summary",Value = nameof(WeatherForecast.Summary),Sortable = false}
    };

    private WeatherForecast[]? forecasts;

    protected override async Task OnInitializedAsync()
    {
        _loading = true;
        forecasts = await ForecastService.GetForecastAsync(DateTime.Now);
        _loading = false;
    }
}
